package com.st.mall.product.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import com.st.mall.product.model.ImgModel;
import com.st.mall.product.service.IImgService;

@Controller
@RequestMapping("/img")
public class ImgController {
	
	@Autowired
	private IImgService service;
	
	private String PATH = "F:/practice/image/";
	
	@RequestMapping("/upload")
	@ResponseBody
	public String upload(HttpServletRequest request, String code) throws IllegalStateException, IOException {
		//将当前上下文初始化给CommonsMultipartResolver(多部分解析器)
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
				request.getSession().getServletContext());
		//检查form中是否有enctype="multipart/form-data"
		if (multipartResolver.isMultipart(request)) {//判断当前request是否有文件上传
			//将request变成多部分request
			MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
			//获取multiRequest中的所有中文名
			Iterator<String> iter = multiRequest.getFileNames();
			//一次遍历所有文件
			while(iter.hasNext()) {
				MultipartFile file = multiRequest.getFile(iter.next().toString());
				if (file == null) {
					continue;
				}
				String ofn = file.getOriginalFilename();
				String nfn = UUID.randomUUID().toString() + ofn.substring(ofn.lastIndexOf("."));
				String path = PATH + nfn;
				file.transferTo(new File(path));//上传-将上传文件写到服务器上指定的文件
				service.insert(new ImgModel(code,nfn,"2"));
			}
		}
		Map<String, String> result = new HashMap<>();
		result.put("code", "0");
		return new JSONObject(result).toString();
	}
	
	@RequestMapping(value = "/getImg", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
	@ResponseBody
	public String seletModel(ImgModel imgModel) {
		System.out.println(imgModel.getCode());
		return new JSONArray(service.selectAll(imgModel)).toString();
	}
	
	@RequestMapping(value = "/setMain", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
	@ResponseBody
	public String setMain(ImgModel imgModel) {
		return service.setMain(imgModel)+"";
	}
	
	@RequestMapping(value = "/imgDel", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
	@ResponseBody
	public String deleteImg(String url) {
		File file = new File(PATH + url);
		file.delete();
		return service.delete(url)+"";
	}
}
